package com.ebt.m.policy.presenter;

import android.content.Context;

import com.ebt.m.AppInitService;
import com.ebt.m.commons.buscomponent.listview.BaseListContract;
import com.ebt.m.commons.buscomponent.listview.BaseListPresenter;
import com.ebt.m.commons.buscomponent.listview.SectionItem;
import com.ebt.m.commons.utils.RxUtil;
import com.ebt.m.data.rxModel.api.EBTAPI;
import com.ebt.m.data.rxModel.apibean.ApiOnlinePolicySearchDataBean;
import com.ebt.m.data.rxModel.apibean.ProductItemsBean;
import com.ebt.m.data.rxModel.apibean.PolicyProposal;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Damon.Han
 */
public class SearchPresenter extends BaseListPresenter<BaseListContract.IView> {

    String wd = null;

    @Override
    public List<SectionItem> createSectionItem(List datas) {
        return null;
    }

    public SearchPresenter(Context context, BaseListContract.IView iView) {
        super(context, iView);
    }

    @Override
    public void loadNew(Object... args) {
        wd = (String) args[0];
        if (wd == null) {
            iView.updateData(null);
            return;
        }
        EBTAPI ebtapi = AppInitService.getEbtApi();
        ebtapi.getOnlinePolicySearch(wd)
                .compose(RxUtil.rxTransformer(iView))
                .subscribe(
                        response -> {
                            iView.updateData(createSectionItem(response.getData()));
                        }, throwable -> {
                            throwable.printStackTrace();
                            iView.errorData();
                        });
    }

    @Override
    public void refresh() {
        loadNew(wd);
    }

    @Override
    public void loadNext(Object... args) {

    }


    public List<SectionItem> createSectionItem(ApiOnlinePolicySearchDataBean apiOnlinePolicySearchDataBean) {
        List<SectionItem> sections = new ArrayList<SectionItem>();
        try {
            List<ProductItemsBean> listProducts = apiOnlinePolicySearchDataBean.getProducts();
            List<PolicyProposal> listProposals = apiOnlinePolicySearchDataBean.getProposals();
            int siezProduct = listProducts.size();
            if (siezProduct > 0) {
                SectionItem item = new SectionItem(0, "产品");
                sections.add(item);

                if (siezProduct > 3) {
                    siezProduct = 3;
                }
                for (int i = 0; i < siezProduct; i++) {
                    ProductItemsBean productInfo = (ProductItemsBean) listProducts.get(i);
                    SectionItem item1 = new SectionItem(1, productInfo);
                    sections.add(item1);
                }
                if (listProducts.size() > 3) {
                    SectionItem item2 = new SectionItem(2, "更多产品");
                    sections.add(item2);
                }
            }

            int siezProposal = listProposals.size();
            if (siezProposal > 0) {
                SectionItem item3 = new SectionItem(0, "计划书");
                sections.add(item3);
                if (siezProposal > 3) {
                    siezProposal = 3;
                }
                for (int i = 0; i < siezProposal; i++) {
                    PolicyProposal ps = (PolicyProposal) listProposals.get(i);
                    SectionItem item4 = new SectionItem(3, ps);
                    item4.extras = ps.name;
                    sections.add(item4);
                }
                if (listProposals.size() > 3) {
                    SectionItem item2 = new SectionItem(2, "更多计划书");
                    sections.add(item2);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return sections;
        }
        return sections;
    }


}
